package com.novanok.micurso;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.novanok.micurso.entidad.Comun;
import com.novanok.micurso.entidad.Curso;

import java.util.Map;

/**
 * Created by Marcos on 07/12/2014.
 */
public class CursosAlumnoActivity extends Activity {

    private AdView adView;

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.cursos_alumno_activity );

        adView = new AdView(this);
        adView.setAdUnitId("ca-app-pub-8819200492193671/2634548741");
        adView.setAdSize(AdSize.BANNER);
        LinearLayout layout = (LinearLayout) findViewById(R.id.banner4);
        layout.addView(adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        if ( Comun.getAlumno() == null ){
            finish();
        }

        Comun.getAlumno().setCursoAlumnoActivity(this);

        ((TextView)findViewById(R.id.lblEmail)).setText( Comun.getAlumno().getEmail() );
        ((TextView)findViewById(R.id.lblNombre)).setText( Comun.getAlumno().getNombre() );
        ((TextView)findViewById(R.id.lblLegajo)).setText( CursosAlumnoActivity.this.getResources().getString(R.string.legajo) + " " + Comun.getAlumno().getLegajo() );

        Firebase.setAndroidContext(this);

        this.cargarMisCursos();
    }

    @Override
    public boolean onCreateOptionsMenu( Menu menu ) {
        getMenuInflater().inflate( R.menu.cursos, menu );
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch ( item.getItemId() ) {
            case R.id.nuevo:
                if ( Comun.isVersionLimitada() ) {
                    if ( Comun.getAlumno().getMisCursos().size() > 0 ) {
                        Mensaje.getMensaje().mostrarInformacion(this, CursosAlumnoActivity.this.getResources().getString(R.string.oneCurso), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                // NADA!!!!!
                            }
                        });
                        return true;
                    }
                }

                Intent i = new Intent(CursosAlumnoActivity.this, CursosAlumnoItemActivity.class);
                startActivity(i);
                return true;

            case R.id.restablecerModo:
                SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
                sharedPref.edit().putString("modo", "").apply();

                Intent a = new Intent(CursosAlumnoActivity.this, MyActivity.class);
                startActivity(a);

                this.finish();

                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void cargarMisCursos(){
        if (Comun.getAlumno().getMisCursos().size() == 0){
            findViewById(R.id.empty).setVisibility(View.VISIBLE);
        }else{
            findViewById(R.id.empty).setVisibility(View.INVISIBLE);
        }

        ((ListView) CursosAlumnoActivity.this.findViewById( R.id.lstCursosAlumno )).setAdapter(
                new ListaCursosAlumnoAdapter(CursosAlumnoActivity.this, Comun.getAlumno().getMisCursos()));
    }

    public void verNotificaciones(View v){
        ListaCursosAlumnoAdapter adapter = ( ListaCursosAlumnoAdapter )( ( ListView ) findViewById( R.id.lstCursosAlumno ) ).getAdapter();
        Curso cursoSeleccionado = adapter.getItem( ( ( ListView ) findViewById( R.id.lstCursosAlumno ) ).getPositionForView( v ) );

        Log.d("TAG", "Legue");

        Intent i = new Intent(this, AlumnoNotificacionActivity.class);
        Bundle b = new Bundle();
        b.putString("curso_seleccionado", cursoSeleccionado.getCurso_id());
        i.putExtras(b);

        startActivity(i);
    }

    public void eliminarInscripcionACurso(View v){
        ListaCursosAlumnoAdapter adapter = ( ListaCursosAlumnoAdapter )( ( ListView ) findViewById( R.id.lstCursosAlumno ) ).getAdapter();
        final Curso cursoSeleccionado = adapter.getItem( ( ( ListView ) findViewById( R.id.lstCursosAlumno ) ).getPositionForView( v ) );

        new AlertDialog.Builder( this )
                .setIcon( android.R.drawable.ic_dialog_info )
                .setTitle( CursosAlumnoActivity.this.getResources().getString(R.string.verificacion) )
                .setMessage( CursosAlumnoActivity.this.getResources().getString( R.string.removeInscription ) )
                .setPositiveButton("SI", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Firebase eliminarMiCurso = new Firebase("https://mi-curso.firebaseio.com/alumno/" + Comun.getAlumno().getId() + "/mis_cursos/" + cursoSeleccionado.getId());
                        eliminarMiCurso.removeValue();

                        Firebase eliminarMiInscripcion = new Firebase("https://mi-curso.firebaseio.com/cursos/" + cursoSeleccionado.getCurso_id() + "/inscripciones/" + Comun.getAlumno().getId());
                        eliminarMiInscripcion.removeValue();
                    }
                })
                .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // Nada
                    }
                })
                .show();
    }
}
